package com.pd.dao;

import com.pd.kit.LogUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;
import java.util.Map;

/**
 * Created by dong peng
 * DATE : 2017/2/26
 * TIME : 23:17.
 */

public class Dao {

    private SessionManager sessionManager;

    public void setSessionManager(SessionManager sessionManager) {
        this.sessionManager = sessionManager;
    }

    /**
     * 查询返回一条记录
     *
     * @param statement
     * @param parameter
     * @param <T>
     * @return
     */
    public <T> T selectOne(String statement, Object parameter) {
        SqlSession session = null;
        T t = null;
        try {
            session = sessionManager.openSqlSession();
            t = session.selectOne(statement,parameter);
        }catch (Exception e){
        }finally {
            if(session != null) session.close();
        }

        return t;
    }


    /**
     * 查询某一列的数据
     *
     * @param statement
     * @param parameter
     * @param <E>
     * @return
     */
    public <E> List<E> selectList(String statement, Object parameter){
        SqlSession session = null;
        List<E> results = null;
        try {
            session = sessionManager.openSqlSession();
            results = session.selectList(statement,parameter);
        }catch (Exception e){
        }finally {
            if(session != null) session.close();
        }

        return results;
    }

    /**
     *查询某一行的数据然后返回map结果集
     *
     * @param statement
     * @param parameter
     * @param mapKey   指定某一参数做key
     * @param <K>
     * @param <V>
     * @return
     */
    public <K,V> Map<K,V> selectMap(String statement, Object parameter, String mapKey) {
        SqlSession session = null;
        Map<K,V> results = null;
        try {
            session = sessionManager.openSqlSession();
            results = session.selectMap(statement,parameter,mapKey);
        }catch (Exception e){
        }finally {
            if(session != null) session.close();
        }

        return results;
    }


    public int insert(String statement, Object parameter) {
        SqlSession session = null;
        int rst = -1;
        try {
            session = sessionManager.openSqlSession();
            rst = session.insert(statement,parameter);
            session.commit();
        }catch (Exception e){
            LogUtil.error("插入数据库数据失败： ",e);
            if(session != null){
                session.rollback();
            }
            if(session != null){
                session.close();
            }
        }finally {
            if(session != null) session.close();
        }

        return rst;
    }


    public int update(String statement, Object parameter) {
        SqlSession session = null;
        int rst = -1;
        try {
            session = sessionManager.openSqlSession();
            rst = session.update(statement,parameter);
            session.commit();
        }catch (Exception e){
            LogUtil.error("更新数据库数据失败： ",e);
            if(session != null){
                session.rollback();
            }
            if(session != null){
                session.close();
            }
        }finally {
            if(session != null) session.close();
        }

        return rst;
    }


    public int delete(String statement, Object parameter) {
        SqlSession session = null;
        int rst = -1;
        try {
            session = sessionManager.openSqlSession();
            rst = session.delete(statement,parameter);
            session.commit();
        }catch (Exception e){
            LogUtil.error("删除数据库数据失败： ",e);
            if(session != null){
                session.rollback();
            }
            if(session != null){
                session.close();
            }
        }finally {
            if(session != null) session.close();
        }

        return rst;
    }
}
